#include "stand_flowdataset.h"

Stand_flowDataset::Stand_flowDataset(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Label_Init();
    ui_Btn_Init();
   connect_Init();
   update_data();
}
void Stand_flowDataset::connect_Init()
{

    //确定
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        keyPad *board = new keyPad(this);
         board->show();
        connect(board,&keyPad::changename2,this,&Stand_flowDataset::Recivedensity);
        connect(board,&keyPad::destroyed,[=](){
            btnlist_ui.at(1)->setFocus();
        });

    });

    //返回
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });

}
void Stand_flowDataset::ui_Label_Init()
{

    for(int i=0;i<11;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 22px;\
                                color: #000000;			                 /* 文本颜色 */\
                           }"
          );
    }
theLabel[0]->setGeometry(50,100,100,40);//显示气体名称
theLabel[1]->setGeometry(160,100,150,40);//显示本页面的功能
theLabel[2]->setGeometry(100,150,120,40);//显示值
theLabel[3]->setGeometry(240,150,100,40);//显示值的数据
theLabel[4]->setGeometry(360,150,100,40);//显示值的单位
theLabel[5]->setGeometry(100,195,120,40);//设置值
theLabel[6]->setGeometry(240,195,100,40);//设置值的数据
theLabel[7]->setGeometry(360,195,100,40);//设置值的单位

theLabel[8]->setStyleSheet("QLabel{"
                      + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
theLabel[9]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
theLabel[10]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

theLabel[8]->setGeometry(0,0,640,60);//设定每隔放置一个按键
theLabel[9]->setGeometry(0,0,300,60);//设定每隔放置一个按键
theLabel[10]->setGeometry(340,0,300,60);//设定每隔放置一个按键
theLabel[8]->setText(Label);

     //显示可设置范围

theLabel[0]->setText(tr(Sysset->Gas[GasSet_ABC-1].gasname));
theLabel[1]->setText(("O3零气流量")+QString::number(GasSet_StandNum)+("总流量："));
theLabel[0]->hide();
theLabel[1]->hide();
//确定可设置范围并显示

theLabel[2]->setText(tr("标准流量："));
theLabel[4]->setText("SCCM");
theLabel[5]->setText(tr("体积流量："));
theLabel[7]->setText("SCCM");


}

void Stand_flowDataset::ui_Btn_Init()
{

    for(int i=0;i<2;i++)//创建主屏幕显示的按键
         theBtn[i] = new QPushButton(this);
btnlist_ui.append(theBtn[0]);
btnlist_ui.append(theBtn[1]);

theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/切换.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/切换2.png); /* 背景图片 */"+"}");
theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

btnlist_ui.at(0)->setGeometry(150,300,120,90 );
btnlist_ui.at(1)->setGeometry(360,300,120,90 );
btnlist_ui.at(0)->setText(tr("确定"));
btnlist_ui.at(1)->setText(tr("返回"));
//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}

void Stand_flowDataset::Recivedensity(QString s)
{
    //必须是在可设置的范围内设置才会有效

    s = QString::number(s.toFloat());
    s=dataprocess(s.toFloat());
    if(s.toFloat()>0)
    {
    switch (flag_datastand)
    {
    case 5:
    {
            Sysset->MFC_Air_Cal.cal_5_FS = s.toFloat()*1000;
            Sysset->MFC_Air_Cal.cal_5_FS_V = s.toFloat()*1000;
    }
        break;
    case 20:
    {
            Sysset->MFC_Air_Cal.cal_20_FS = s.toFloat()*1000;
            Sysset->MFC_Air_Cal.cal_20_FS_V = s.toFloat()*1000;
    }
        break;
    case 35:
    {
           Sysset->MFC_Air_Cal.cal_35_FS = s.toFloat()*1000;
           Sysset->MFC_Air_Cal.cal_35_FS_V = s.toFloat()*1000;
    }
        break;
    case 50:
    {
            Sysset->MFC_Air_Cal.cal_50_FS = s.toFloat()*1000;
            Sysset->MFC_Air_Cal.cal_50_FS_V = s.toFloat()*1000;
    }
        break;
    case 65:
    {
            Sysset->MFC_Air_Cal.cal_65_FS = s.toFloat()*1000;
            Sysset->MFC_Air_Cal.cal_65_FS_V = s.toFloat()*1000;
    }
        break;
    case 80:
    {
            Sysset->MFC_Air_Cal.cal_80_FS = s.toFloat()*1000;
            Sysset->MFC_Air_Cal.cal_80_FS_V = s.toFloat()*1000;
    }
        break;
    case 95:
    {
            Sysset->MFC_Air_Cal.cal_95_FS = s.toFloat()*1000;
            Sysset->MFC_Air_Cal.cal_95_FS_V = s.toFloat()*1000;
    }
    default:
        break;
    }
    theLabel[3]->setText(QString::number(s.toFloat()));
    theLabel[6]->setText(QString::number(s.toFloat()));
    Sysctrl->Para_Save = 1;
    }

}

bool Stand_flowDataset::eventFilter(QObject *watched, QEvent *event)
{
    if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched)
{
    if(event->type() == QEvent::KeyPress)
    {
        QKeyEvent *ev = (QKeyEvent *)event;
        QPushButton *button = (QPushButton *)watched;

       {
           switch (ev->key())
           {
           case OK:
           {
               emit  button-> clicked();
             break;
           }
           case UP: //Up
             {

               focusPreviousChild();
               break;
             }
           case DOWN: //Down
             {
              focusNextChild();
               break;
             }
           case BACK:
           {
               this->parentWidget()->show();
               this->close();
               break;
           }
           case INIT:
           {
               init_Show *show = new init_Show(this);
               show->show();
               this->hide();

               break;
           }
            default:
               break;
           }
       }
    }
    }
    return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}
QString Stand_flowDataset::dataprocess(float num)
{
    float test_temp = num*1000 - (int)(num*1000); //1000 isnot  range
    if(test_temp==0) //less 3
         data_re=QString::number(num);
    else
        data_re =QString::number(num,'f',3);
    return data_re;
}
void Stand_flowDataset::update_data()
{
    //显示
    switch (flag_datastand) {
    case 5:
    {
        Label=("5%");
        Sysctrl->Cal_Span_Sel =1;
        QString showdata = dataprocess(Sysset->MFC_Air_Cal.cal_5_FS/1000);
        theLabel[3]->setText(showdata);
        theLabel[6]->setText(showdata);
    }
        break;
    case 20:
    {
        Label=("20%");
        Sysctrl->Cal_Span_Sel =2;
        QString showdata = dataprocess(Sysset->MFC_Air_Cal.cal_20_FS/1000);
        theLabel[3]->setText(showdata);
        theLabel[6]->setText(showdata);
    }
        break;
    case 35:
    {
        Label=("35%");
        Sysctrl->Cal_Span_Sel =3;
        QString showdata = dataprocess(Sysset->MFC_Air_Cal.cal_35_FS/1000);
        theLabel[3]->setText(showdata);
        theLabel[6]->setText(showdata);
    }
        break;
    case 50:
    {
        Label=("50%");
        Sysctrl->Cal_Span_Sel =4;
        QString showdata = dataprocess(Sysset->MFC_Air_Cal.cal_50_FS/1000);
        theLabel[3]->setText(showdata);
        theLabel[6]->setText(showdata);
    }
        break;
    case 65:
    {
        Label=("65%");
        Sysctrl->Cal_Span_Sel =5;
        QString showdata = dataprocess(Sysset->MFC_Air_Cal.cal_65_FS/1000);
        theLabel[3]->setText(showdata);
        theLabel[6]->setText(showdata);
    }
        break;
    case 80:
    {
        Label=("80%");
        Sysctrl->Cal_Span_Sel =6;
        QString showdata = dataprocess(Sysset->MFC_Air_Cal.cal_80_FS/1000);
        theLabel[3]->setText(showdata);
        theLabel[6]->setText(showdata);
    }
        break;
    case 95:
    {
        Label=("95%");
        Sysctrl->Cal_Span_Sel =7;
        QString showdata = dataprocess(Sysset->MFC_Air_Cal.cal_95_FS/1000);
        theLabel[3]->setText(showdata);
        theLabel[6]->setText(showdata);
    }
        break;
    default:
        break;
    }
}
